/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vetxsoft.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import vetxsoft.bean.ControlBean;
import vetxsoft.entity.HistoriaClinica;
import vetxsoft.system.IVetexsoftSystem;

/**
 *
 * @author Nother
 */
@FacesConverter(forClass = HistoriaClinica.class, value = "HistoriaConvertor")
public class HistoriaConvertor implements Converter {

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if (value == null || value.trim().equalsIgnoreCase("Seleccione...") || value.trim().length() == 0) {
            return null;
        }
        try {
             IVetexsoftSystem vetexsoftSystem = ((ControlBean) fc.getApplication().getELResolver().getValue(fc.getELContext(), null, "controlBean")).getVetexsoftSystem();
             HistoriaClinica historiaClinica = vetexsoftSystem.buscarHistoriaClinica(value);
             return historiaClinica;
        }catch (Exception _exc) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof HistoriaClinica) {
            HistoriaClinica historiaClinica = (HistoriaClinica) value;
            return historiaClinica.getCodigo() + "";
        } else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: " + HistoriaClinica.class.getName());
        }
    }
}
